<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <%@include file="/WEB-INF/jsp/common.jsp"%>
</head>
<body>
<h2>药品管理</h2>
<hr>
<form action="" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">药品名称：</label>
            <div class="layui-input-inline">
                <input type="text" id="ypname" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm" onclick="reloadData();"><i class="layui-icon layui-icon-search"></i> 搜索</button>
        </div>
    </div>
</form>
<table id="ypglTable" lay-filter="ypglTable"></table>
<script>
    function reloadData(){
        layui.table.reload("ypglTable",{
            page:{
                curr : 1
            },
            where : {
                ypname : $("#ypname").val(),
            }
        })
    }

    layui.use(['table','form','layedit'],function () {
        var table= layui.table;
        var form = layui.form;
        var layedit = layui.layedit;

        //渲染表格
        table.render({
            elem: '#ypglTable',
            url: '${path}/ypgl/list',
            page:true,
            even: true,
            toolbar:'#toolbarDemo',
            cols:[[
                {type:'radio'},
                {field:'ypid',title:'标号'},
                {field:'ypname',title:'药品名称'},
                {field:'zyc',title:'制药厂'},
                {field:'ypdw',title:'药品单位',},
                {field:'ypshuoming',title:'药品说明'},
                {field:'yplsj',title:'药品零售价'},
                {field:'ypkc',title:'药品库存'},
                {field:'hello', title:'操作',toolbar:'#barDemo'}
            ]]
        })

        table.on('toolbar(ypglTable)',function (obj) {
            var e = obj.event;

            switch (e) {
                case 'add':
                    $.get('${path}/ypgl/form',function (str) {
                        layer.open({
                            type: 1,
                            title: '药品添加',
                            area: ['100%','100%'],
                            maxmin: true,
                            content : str,
                            btn:['确定','取消'],
                            yes:function (index) {
                                layui.layedit.sync(index)
                                var params = $("#add_ypgl_form").serialize();
                                console.log(params)
                                $.post('${path}/ypgl/add',params,function () {
                                    table.reload('ypglTable');
                                    layer.close(index);
                                })
                            }
                        })
                    })
                    break;
            }
        })

        table.on("tool(ypglTable)",function (obj) {
            var data = obj.data;
            var e = obj.event;
            if (e == "edit"){
                $.get("${path}/ypgl/form",function (str) {
                    $.get('${path}/ypgl/view',{ypid:data.ypid},function (data) {
                        var index = layer.open({
                            type: 1,
                            title:"修改药品",
                            content: str,
                            area: ['100%','100%'],
                            maxmin: true,
                            btn:['确定','取消'],
                            success:function(){
                                form.val('userForm',data);
                            },
                            yes:function (index) {
                                layui.layedit.sync(index)
                                var params=$('#add_ypgl_form').serialize();
                                console.log(params)
                                $.post('${path}/ypgl/edit',params,function () {
                                    table.reload("ypglTable")
                                    layer.close(index);
                                    layer.msg("修改成功",function () {

                                    })
                                })
                            }
                        });
                    })
                })
            }else if (e == "del"){
                layer.open({
                    title: "友情提示",
                    content:"你确定删除吗?",
                    btn:['确定','取消'],
                    yes:function () {
                        $.post("${path}/ypgl/batchDelete",{ids:data.ypid},function () {
                            layer.msg("删除成功了");
                            table.reload('ypglTable');
                        });
                    }
                })
            }
        })
    })
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <shiro:hasPermission name="yaopin:add">
            <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i> 添加</button>
        </shiro:hasPermission>
    </div>
</script>
<script type="text/html" id="barDemo">
    <shiro:hasPermission name="yaopin:upd">
        <a href="javascript:void(0)" lay-event="edit"><i class="layui-icon layui-icon-edit"></i> </a>
    </shiro:hasPermission>
    <shiro:hasPermission name="yaopin:del">
        <a href="javascript:void(0)" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
    </shiro:hasPermission>
</script>
</body>
</html>
